package com.cyou.qb.common.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.cyou.nad.datastore.query.JdbcPage;
import com.cyou.nad.datastore.query.JdbcPageDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cyou.qb.common.dao.BlackListDao;
import com.cyou.qb.common.domain.BlackList;
import com.cyou.qb.common.service.BlackListService;
import com.cyou.qb.exception.ServiceException;

@Service("blackListServiceImpl")
public class BlackListServiceImpl extends BaseSerivceImpl implements BlackListService {

	@Autowired
	private BlackListDao blackListDao;

	@Autowired
	private JdbcPageDao jdbcPageDao;

	@Override
	public List<BlackList> findBlackList(String passport, Date curr) {
		List<BlackList> blackList = blackListDao.findBlackList(passport, curr);
		if (blackList == null) {
			throw new ServiceException("查询黑名单失败，数据库错误");
		}
		return blackList;
	}

	@Override
	public List<BlackList> findAllValidBlackList() {
		return blackListDao.findAllValidBlackList();
	}

	@Override
	public int saveBlackList(int uCenterId, String passport, String nickName, Date start, Date end, String forbidden,
			String memo) {
		BlackList blackList = new BlackList(uCenterId, passport, nickName, start, end, forbidden, memo, new Date());
		int count = blackListDao.saveOneBlackList(blackList);
		this.getServiceContext().addLog(passport, blackList.getId(), "t_black_list", "添加黑名单", new Object[]{blackList}, null);
		return count;
	}

	@Override
	public JdbcPage findPageQueryBlackList(String passport, String nickname, int pageNo, int pageSize) {
		String sql = "\nselect id, start_time, end_time, nickname, memo, create_date, u_center_id, passport"
				+ "\n from t_black_list " + "\n where 1=1";
		List<Object> params = new ArrayList<Object>();
		//组装查询条件
		if (!"".equals(passport)) {
			sql += "\n and passport like ? ";
			params.add("%" + passport + "%");
		}
		if (!"".equals(nickname)) {
			sql += "\n and nickname like ? ";
			params.add("%" + nickname + "%");
		}
		sql += "\n order by create_date desc";
		//return
		return jdbcPageDao.pageQuery(sql, pageNo, pageSize, params.toArray());
	}

	public void removeBlackList(int blackListId) throws Exception {
		int count = 0;
		count = blackListDao.deleteBlackListById(blackListId);
		if (count == 0) {
			throw new ServiceException("删除失败");
		}
		this.getServiceContext().addLog("", blackListId, "t_black_list", "删除黑名单", null, new Object[]{blackListId});
	}

	public BlackListDao getBlackListDao() {
		return blackListDao;
	}

	public void setBlackListDao(BlackListDao blackListDao) {
		this.blackListDao = blackListDao;
	}

	public JdbcPageDao getJdbcPageDao() {
		return jdbcPageDao;
	}

	public void setJdbcPageDao(JdbcPageDao jdbcPageDao) {
		this.jdbcPageDao = jdbcPageDao;
	}

}
